//
//  NewTVController.m
//  GBKHOME
//
//  Created by XMYY-19 on 2017/9/5.
//
//

#import "NewTVController.h"
#import "Global1.h"
#import "tvView.h"
#import "LoginController1.h"
#define PIC_WIDTH 200
#define PIC_HEIGHT 200
#define COL_COUNT 3
@interface NewTVController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (retain, nonatomic) IBOutlet UIButton *GmenuBtn;
@property (retain, nonatomic) IBOutlet UIButton *GbackBtn;
@property (retain, nonatomic) IBOutlet UIButton *tvavPro;
@property (retain, nonatomic) IBOutlet UIButton *volicBtn;
@property (retain, nonatomic) IBOutlet UIButton *channelBtn;
- (IBAction)menuClick:(UIButton *)sender;
- (IBAction)channel_Click:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIButton *tvBtn;
- (IBAction)tv_kaiguanClick:(UIButton *)sender;
- (IBAction)vocUp_click:(UIButton *)sender;
- (IBAction)vocDown_click:(UIButton *)sender;
- (IBAction)chanelUp_click:(UIButton *)sender;
- (IBAction)chanelDown_click:(UIButton *)sender;
- (IBAction)okClick:(UIButton *)sender;
- (IBAction)tvAVClick:(UIButton *)sender;
- (IBAction)volClick:(UIButton *)sender;
- (IBAction)goBackClick:(UIButton *)sender;

@property(nonatomic,strong)UIView *myTVView;

@property(nonatomic,assign)BOOL isClick;
@property(nonatomic,assign)BOOL isClosed;

@property(nonatomic,strong)UICollectionView *keyBoradCollVC;
#define SFdentifiter @"cellID"
- (IBAction)TVClick:(UIButton *)sender;
@end
@implementation NewTVController

-(id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self)
    {
        self.isClosed = NO;
        self.equipment = equ;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setNavigationTitleView:_equipment.name];
    
    UIImage *image =[UIImage imageNamed:@"mainbg"];
    self.view.layer.contents = (id)image.CGImage;
    // self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"mainbg"]];
    
    //self.mytvView = [[tvView alloc] init];
    //[tvView secView];
    // self.mytvView.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"keyboard_bg"]];
    //    [self.view addSubview:self.mytvView];
    
    
    
    // [self addAllButtons];
    //[self initLeftCollectionView];
    
    
    
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"tvView" owner:self options:nil];
    UIView *plainView = [nibContents lastObject];
    
    // CGSize padding = (CGSize){ 22.0, 22.0 };
    plainView.frame = CGRectMake(0, 667, 375, 667);
    
    self.myTVView = plainView;
    
    [self.view addSubview:plainView];
    
    
     [self.tvBtn setBackgroundImage:[UIImage imageNamed:@"btn_power_off"] forState:UIControlStateNormal];
    
    
    [self initUI];
}

-(void)initUI
{
    [self.channelBtn setBackgroundColor:[UIColor colorWithRGBColorByString:@"#17174c"]];
    self.channelBtn.layer.cornerRadius = 20;
    
    [self.channelBtn setTitleColor:[UIColor colorWithRGBColorByString:@"#8e8ea0"] forState:UIControlStateNormal];
    
    [self.volicBtn setBackgroundImage:[UIImage imageNamed:@"sound_off"] forState:UIControlStateNormal];
    
    [self.tvavPro setBackgroundColor:[UIColor colorWithRGBColorByString:@"#17174c"]];
    [self.tvavPro setTitleColor:[UIColor colorWithRGBColorByString:@"#8e8ea0"] forState:UIControlStateNormal];
    
     [self.GbackBtn setBackgroundColor:[UIColor colorWithRGBColorByString:@"#17174c"]];
       [self.GbackBtn setTitleColor:[UIColor colorWithRGBColorByString:@"#8e8ea0"] forState:UIControlStateNormal];
    
    [self.GmenuBtn setBackgroundColor:[UIColor colorWithRGBColorByString:@"#17174c"]];
    [self.GmenuBtn setTitleColor:[UIColor colorWithRGBColorByString:@"#8e8ea0"] forState:UIControlStateNormal];
    
}
- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
}
//-(tvView *)mytvView
//{
//  if(!_mytvView)
//  {
//      tvView *tvView = [tvView secView];
//  }
//}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //self.mytvView.frame = CGRectMake(0, 300, 375, 667);
}

/**
 九宫格布局
 */
//-(void)addAllButtons
//{
//
//
//
//    int totalloc=3;
//    CGFloat appvieww=200;
//    CGFloat appviewh=200;
//
//    CGFloat margin=(self.view.frame.size.width-totalloc*appvieww)/(totalloc+1);
//
//    int count=9;
//    //    UIScrollView *myView =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 330, screenWide, 300)];
//    //    myView.backgroundColor =[UIColor clearColor];
//    //    myView.contentSize = CGSizeMake(screenWide, 500);
//    //    // 隐藏水平滚动条
//    //       myView.showsHorizontalScrollIndicator = NO;
//    //         myView.showsVerticalScrollIndicator = NO;
//    //    [self.navigationController.view addSubview:myView];
//
//
//
//    for(int i=0;i<count;i++)
//    {
//
//        int row=i/totalloc;//行号
//        //1/3=0,2/3=0,3/3=1;
//        int loc=i%totalloc;//列号
//
//        CGFloat appviewx=margin+(margin+appvieww)*loc;
//        CGFloat appviewy=margin+(margin+appviewh)*row;
//
//
//        UIButton *mybutton = [[UIButton alloc] init];
//        mybutton.layer.cornerRadius = 100;
//        [mybutton setBackgroundImage:[UIImage imageNamed:@"keyboard_on"] forState:UIControlStateNormal];
//        [mybutton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//        [mybutton setTitle:@"1" forState:UIControlStateNormal];
//        [self.myTVView addSubview:mybutton];
//
//
//        //        NSInteger row = i / COL_COUNT;
//        //
//        //        NSInteger col = i % COL_COUNT;
//        //
//        //        CGFloat margin = (self.view.frame.size.width - (kFONT_SIZE_FROM_PX_VALUE(PIC_WIDTH) * COL_COUNT)) / (COL_COUNT + 1);
//        //
//        //        CGFloat picX = margin + (kFONT_SIZE_FROM_PX_VALUE(PIC_WIDTH) + margin) * col;
//        //
//        //        CGFloat picY = margin + (kFONT_SIZE_FROM_PX_VALUE(PIC_HEIGHT) + margin) * row;
//
//        mybutton.frame = CGRectMake(appviewx, appviewy, kFONT_SIZE_FROM_PX_VALUE(appvieww), kFONT_SIZE_FROM_PX_VALUE(appviewh));
//
//    }
//
//}
- (IBAction)TVClick:(UIButton *)sender {
    [UIView animateWithDuration:0.5 animations:^{
        // 设置view弹出来的位置
        self.myTVView.frame = CGRectMake(-20, 50, 375, 667);
    }];
}
#pragma mark--打开电视开关按钮
- (IBAction)tv_kaiguanClick:(UIButton *)sender {
    
    NSString *order = @"";
    NSString *switchId = self.equipment.switchId;
    if(self.isClosed)
    {
        self.isClosed = NO;
        order = @"CLOSE";
    }
    else
    {
        self.isClosed = YES;
        order = @"OPEN";
        
    }
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlDevice" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceEnd:dic];
    }];
    
    
    UIButton *button = sender;
    self.tvBtn = button;
    if(_isClick)
    {
        [self.tvBtn setBackgroundImage:[UIImage imageNamed:@"btn_power_off"] forState:UIControlStateNormal];
        _isClick = NO;
    }
    else
    {
        [self.tvBtn setBackgroundImage:[UIImage imageNamed:@"btn_power_on"] forState:UIControlStateNormal];
        _isClick = YES;
    }
}

#pragma mark - 电器设备开关状态
- (void)ctlDevice
{
    NSString *switchId = self.equipment.switchId;
    NSString *name = self.equipment.name;
    BOOL STATE = [DeviceView deviceStateOn:self.equipment];
    BOOL OPEN = !STATE;
    [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
}
- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    if (self.equipment == nil) {//后台刷新电器状态，更新状态，已重载电器，此电器已释放
        NSLog(@"nil");
        return;
    }
    NSString *tip = @"";
    BOOL OPEN = [DeviceView deviceStateOn:self.equipment];
    BOOL STATE = !OPEN;
    NSNumber *result = [dic objectForKey:@"result"];
    if (result.integerValue == 0) {
        
        tip = [OperationTip operat:self.equipment.name open:STATE result:YES errorType:ErrorTypeServerReturnFail];
        [self setDeviceOn:STATE];
        
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNet];
    }
    else
    {
        tip = [OperationTip operat:self.equipment.name open:STATE result:NO errorType:ErrorTypeServerReturnFail];
    }
    
    [iToast showTip:tip gravity:iToastGravityBottom];
}
- (void)setDeviceOn:(BOOL)on
{
    //    self.Switch = on;
    //    self.button.selected = on;
    if (on)
    {
        //        [self.button setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
        self.equipment.state = kStateON;
    }
    else
    {
        //        [self.button setBackgroundImage:[UIImage imageNamed:@"lightBlue.png"] forState:UIControlStateNormal];
        self.equipment.state = kStateOFF;
    }
    // [self updateProtectName];
}
- (void)updateState:(NSArray*)states
{
    [GKBMainController updateEqu:self.equipment button:self.tvBtn states:states];
}
- (IBAction)vocUp_click:(UIButton *)sender {
    
    NSString *order = @"VOLJIA";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"音量+键"];
    }];
 
}

- (IBAction)vocDown_click:(UIButton *)sender {
    
    NSString *order = @"VOLJIAN";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"音量-键"];
    }];
}

- (IBAction)chanelUp_click:(UIButton *)sender
{
    NSString *order = @"CHJIA";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"频道+键"];
    }];
}
- (IBAction)chanelDown_click:(UIButton *)sender {
    
    NSString *order = @"CHJIAN";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"频道-键"];
    }];
}
- (IBAction)okClick:(UIButton *)sender
{
    
    NSString *order = @"OK";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"OK键"];
    }];
}

-(void)ctlDeviceInfo:(NSDictionary *)dict tipName:(NSString *)tipName
{
    if (self.equipment == nil) {//后台刷新电器状态，更新状态，已重载电器，此电器已释放
        NSLog(@"nil");
        return;
    }
    NSString *tip = @"";
    BOOL OPEN = [DeviceView deviceStateOn:self.equipment];
    BOOL STATE = !OPEN;
    NSNumber *result = [dict objectForKey:@"result"];
    if (result.integerValue == 0) {
        
        tip = [OperationTip operat:tipName open:STATE result:YES errorType:ErrorTypeServerReturnFail];
        [self setDeviceOn:STATE];
        
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNet];
    }
    else
    {
        tip = [OperationTip operat:self.equipment.name open:STATE result:NO errorType:ErrorTypeServerReturnFail];
    }
    
    [iToast showTip:tip gravity:iToastGravityBottom];
}


- (IBAction)tvAVClick:(UIButton *)sender {
    NSString *order = @"T/AV";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"T/AV键"];
    }];
  
}
- (IBAction)volClick:(UIButton *)sender {
    
   
}
- (IBAction)goBackClick:(UIButton *)sender {
    NSString *order = @"BACK";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"BACK键"];
    }];
}
- (void)dealloc {
    [_tvBtn release];
    [_channelBtn release];
    [_volicBtn release];
    [_tvavPro release];
    [_GbackBtn release];
    [_GmenuBtn release];
    [super dealloc];
}
- (IBAction)menuClick:(UIButton *)sender {
    
    NSString *order = @"MENU";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"MENU键"];
    }];
}
- (IBAction)channel_Click:(UIButton *)sender
{
    
    NSString *order = @"-/--";
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    [WebServices setShowConnectting:NO];
    [WebServices requestSynStartOrder:@"ctlTV" body:body result:^(NSDictionary *dic) {
        [self ctlDeviceInfo:dic tipName:@"-/--键"];
    }];
}
@end
